/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.com.nextel.main;

//import com.nextel.gui.dialog.DialogBoxListener;
import com.nextel.ui.OCommandActionListener;
import com.nextel.ui.OCompositeScreen;
import com.nextel.ui.OHandset;
import com.nextel.ui.OSoftKey;
import com.nextel.ui.OTextScreen;
import com.nextel.ui.OUILook;
import com.nextel.ui.custom.ODialogListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Item;
import pe.com.nextel.http.MensajeHttp;
import pe.com.nextel.idioma.IdiomaManager;

/**
 *
 * @author eespinoza
 */

public abstract class ControlBase {

    private MainMidlet action;
    
    private OCompositeScreen display;
        
    private OTextScreen textBox;
    
    final OSoftKey cmdOk = new OSoftKey("OK");
    final OSoftKey cmdBack = new OSoftKey("");
    ODialogListener dialogListener;
    

    

    public ControlBase() {        
        //System.out.println("java_mensaje:" + IdiomaManager.getInstance().getMensaje(IdiomaManager.JAVA_MENSAJE));
        
        textBox = new OTextScreen(IdiomaManager.getInstance().getMensaje(IdiomaManager.JAVA_MENSAJE), OUILook.PLAIN_SMALL_TITLE," ", OUILook.PLAIN_SMALL_TITLE);
        
        textBox.setComandListener(new OCommandActionListener() {

            public void performAction(OSoftKey c) {
                if (c == cmdOk) {
                    OHandset.getDisplay().setCurrent(display);

                }
            }
        });

        textBox.addSoftKey(cmdOk);
        textBox.addSoftKey(cmdBack);
        
    }
    
    public ActionListener getAction(){
        return (ActionListener )action;
    }

    public void setAction(MainMidlet paction) {

        action = paction;
        
    }

    public void setCurrent(MVCAction mvc) {
        OHandset.getDisplay().setCurrent(mvc.showScreen());
    }

    public void setCurrent(OCompositeScreen display) {
        OHandset.getDisplay().setCurrent(display);
    }

    public void showError(String message, OCompositeScreen displayp) {
        //System.out.println("error " + message); 
        showGeneral(message, "/res/error.PNG", displayp);

    }

    public void showMensaje(String message, OCompositeScreen displayp) {

        showGeneral(message, "/res/mensaje.PNG", displayp);

    }

    private void showGeneral(String message, String imagen, OCompositeScreen displayp) {
        this.display = displayp;
        this.display.limpiarFormulario = false;
        
        textBox.setNombreImagen(imagen);
        textBox.setText(message);
        
        OHandset.getDisplay().setCurrent(textBox);
        
        
    }

    public void showConfirmacion(String message, ODialogListener plistener, OCompositeScreen displayp) {

        
        this.dialogListener = plistener;
        displayp.limpiarFormulario = false;
        final OSoftKey cmdSi = new OSoftKey(IdiomaManager.getInstance().getMensaje(IdiomaManager.JAVA_SI));
        final OSoftKey cmdNo = new OSoftKey(IdiomaManager.getInstance().getMensaje(IdiomaManager.JAVA_NO));


        OTextScreen textBoxDialogo = new OTextScreen(IdiomaManager.getInstance().getMensaje(IdiomaManager.JAVA_MENSAJE), OUILook.PLAIN_SMALL_TITLE, message, OUILook.PLAIN_SMALL_TITLE);
        textBoxDialogo.setNombreImagen("/res/confirmacion.PNG");

        textBoxDialogo.setComandListener(new OCommandActionListener() {

            public void performAction(OSoftKey c) {
                if (c == cmdSi) {
                    dialogListener.botonOK();
                }
                if (c == cmdNo) {
                    dialogListener.botonCancel();
                }
            }
        });

        textBoxDialogo.addSoftKey(cmdSi);
        textBoxDialogo.addSoftKey(cmdNo);

        OHandset.getDisplay().setCurrent(textBoxDialogo);

        //dialogListener = null;
    }

    public void exitBussiness() {
        action.exitBussiness();
    }

    public void iniciarConexionHTTP() {
        action.iniciarConexionHTTP();
    }

    public MensajeHttp getMensajeHttp() {
        return action.getMensajeHttp();
    }

    /********************************************************************/
    public void setCurrent(Displayable display) {
        OHandset.getDisplay().setCurrent(display);
    }

    public void setCurrentItem(Item display) {
        OHandset.getDisplay().setCurrentItem(display);
    }

    public void showMensaje(Object message, Object displayp) {
    }

}
